package com.xbongbong.saas.help.workflow;

import com.xbongbong.paas.enums.StageTypeEnum;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.entity.CustomerStageEntity;
import com.xbongbong.saas.domain.entity.CustomerStageLogEntity;
import com.xbongbong.saas.model.CustomerStageLogModel;
import com.xbongbong.saas.model.CustomerStageModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 *
 */
@Component
public class CustomerStageLogHelp {

    @Resource
    private CustomerStageLogModel customerStageLogModel;
    @Resource
    private CustomerStageModel customerStageModel;

    /**
     * 编辑客户的时候，如果阶段没有发生变更，但是修改了流失原因，那么需要将修改后的流失原因同步到阶段记录中
     *
     * @param corpid
     * @param formId
     * @param dataId
     * @param stageCode
     * @param reasonId
     * @param wastageMemo
     */
    public void updateStageLogReasonId(String corpid, Long formId, Long dataId, String stageCode, Integer reasonId, String wastageMemo) {
        CustomerStageEntity targetStage = null;
        if (StringUtil.isNotEmpty(stageCode)) {
            targetStage = customerStageModel.getByCode(corpid, formId, stageCode);
        }
        // bugId:34355 编辑客户的时候，如果阶段没有发生变更，但是修改了流失原因，那么需要将修改后的流失原因同步到阶段记录中
        if(Objects.nonNull(targetStage) && Objects.equals(targetStage.getType(), StageTypeEnum.FAIL_STAGE.getType())) {
            Long now = DateTimeUtil.getInt();
            //获取下一阶段是否已有记录
            Map<String, Object> param = new HashMap<>(16);
            param.put("dataId", dataId);
            param.put("corpid", corpid);
            param.put("stageCode", stageCode);
            param.put("del", 0);
            List<CustomerStageLogEntity> stageLogList = customerStageLogModel.findEntitys(param);
            CustomerStageLogEntity customerStageLogEntity;
            if (stageLogList.isEmpty()) {
                customerStageLogEntity = new CustomerStageLogEntity();
                customerStageLogEntity.setCorpid(corpid);
                customerStageLogEntity.setFormId(formId);
                customerStageLogEntity.setStageProcessId(targetStage.getStageProcessId());
                customerStageLogEntity.setStageId(targetStage.getId());
                customerStageLogEntity.setStageType(targetStage.getType());
                customerStageLogEntity.setStageCode(stageCode);
                customerStageLogEntity.setDataId(dataId);
                customerStageLogEntity.setAddTime(now);
                customerStageLogEntity.setHistoryStayTime(0L);
                customerStageLogEntity.setIntoTime(now);
            } else {
                customerStageLogEntity = stageLogList.get(0);
            }
            customerStageLogEntity.setIsNow(1);
            customerStageLogEntity.setReasonId(reasonId);
            customerStageLogEntity.setMemo(wastageMemo);
            customerStageLogEntity.setUpdateTime(now);
            customerStageLogEntity.setDel(0);
            customerStageLogEntity.setOutTime(0L);
            customerStageLogModel.save(customerStageLogEntity);
        }
    }
}
